package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.entity.SelectMediaEntity;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.SelAudioAdapter;
import flc.ast.databinding.ActivitySelAudioBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.media.MediaLoader;
import stark.common.basic.media.bean.AudioBean;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class SelAudioActivity extends BaseAc<ActivitySelAudioBinding> {

    public static int type;
    private SelAudioAdapter audioAdapter;
    private String selPath = "";
    private List<String> listPath = new ArrayList<>();
    private int oldPosition = 0;
    private boolean isSelectAll = false;
    private String again = "";


    @Override
    protected int onCreate() {
        return R.layout.activity_sel_audio;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSelAudioBack.setOnClickListener(v -> onBackPressed());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 3);
        mDataBinding.rvAudioList.setLayoutManager(gridLayoutManager);
        audioAdapter = new SelAudioAdapter();
        audioAdapter.setGridLayoutManager(gridLayoutManager);
        audioAdapter.setEdit(true);
        audioAdapter.setNoManage(true);
        mDataBinding.rvAudioList.setAdapter(audioAdapter);
        audioAdapter.setOnItemClickListener(this);
        mDataBinding.llSelectAll.setOnClickListener(this);
        mDataBinding.tvConfirm.setOnClickListener(this);
        //音频拼接再次进入选择
        again = getIntent().getStringExtra(Content.AgainAudio);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llSelectAll:
                listPath.clear();
                if (isSelectAll) {
                    for (AudioBean validDatum : audioAdapter.getValidData()) {
                        validDatum.setSelected(false);
                    }
                } else {
                    for (AudioBean validDatum : audioAdapter.getValidData()) {
                        validDatum.setSelected(true);
                        listPath.add(validDatum.getPath());
                    }
                }
                isSelectAll();
                audioAdapter.notifyDataSetChanged();
                break;
            case R.id.tvConfirm:
                if (selPath.isEmpty() && listPath.size() == 0) {
                    ToastUtils.showShort(getString(R.string.please_sel_audio));
                    return;
                }
                if (type == 0) {
                    if (TextUtils.isEmpty(again)) {
//                        AudioSpliceActivity.start(mContext, selPath);
                        List<String> list = new ArrayList<>();
                        for (String s : listPath) {
                            list.add(s);
                        }
                        AudioSpliceActivity.mAddList = list;
                        startActivity(AudioSpliceActivity.class);
                    } else {
                        //添加音频
                        Intent intent = new Intent();
                        intent.putExtra(Content.audioPath, selPath);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                } else {
                    if (listPath.size() > 0) {
                        ToastUtils.showShort(getString(R.string.please_sel_one_audio));
                        return;
                    }
                    if (type == 1) {
                        AudioFormatActivity.audioPath = selPath;
                        startActivity(AudioFormatActivity.class);
                    } else if (type == 2) {
                        AudioSpeedActivity.audioPath = selPath;
                        startActivity(AudioSpeedActivity.class);
                    }
                }
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (type == 0) {
            if (TextUtils.isEmpty(again)) {
                if (audioAdapter.getItem(position).isSelected()) {
                    audioAdapter.getItem(position).setSelected(false);
                    listPath.remove(audioAdapter.getItem(position).getPath());
                } else {
                    audioAdapter.getItem(position).setSelected(true);
                    listPath.add(audioAdapter.getItem(position).getPath());
                }
                audioAdapter.notifyItemChanged(position);
                isSelectAll();
            } else {
                audioAdapter.getItem(oldPosition).setSelected(false);
                audioAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                audioAdapter.getItem(position).setSelected(true);
                audioAdapter.notifyItemChanged(position);
                selPath = audioAdapter.getItem(position).getPath();
                mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 1));
            }
        } else {
            audioAdapter.getItem(oldPosition).setSelected(false);
            audioAdapter.notifyItemChanged(oldPosition);
            oldPosition = position;
            audioAdapter.getItem(position).setSelected(true);
            audioAdapter.notifyItemChanged(position);
            selPath = audioAdapter.getItem(position).getPath();
            mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 1));
        }
    }

    @Override
    protected void initData() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, 0));
        getPermission();
    }

    //获取权限
    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_AUDIO)
                .reqPermissionDesc(getString(R.string.get_music_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        ToastUtils.showShort(R.string.no_permission);
                    }
                }).request();
    }

    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<AudioBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<AudioBean>> emitter) {
                List<AudioBean> audioBeans = MediaLoader.loadAudio();
                emitter.onNext(audioBeans);
            }

            @Override
            public void accept(List<AudioBean> audioBeanList) {
                if (audioBeanList != null) {
                    audioAdapter.setList(audioBeanList);
                    mDataBinding.rvAudioList.setVisibility(View.VISIBLE);
                    mDataBinding.ivSelAudioNoData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvAudioList.setVisibility(View.GONE);
                    mDataBinding.ivSelAudioNoData.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void isSelectAll() {
        mDataBinding.tvSelNum.setText(getString(R.string.sel_num, listPath.size()));
        if (listPath.size() == audioAdapter.getValidData().size()) {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.ayiquanx);
            isSelectAll = true;
        } else {
            mDataBinding.ivSelectAll.setImageResource(R.drawable.aquanx);
            isSelectAll = false;
        }
    }

}